<?
require_once("./include/ProcessDB.inc");
include_once( "include/global.php" );
require_once("./include/head.inc");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>類別管理</title>
<script LANGUAGE="JavaScript" type="text/JavaScript"> 
<!--
function DeleteField( form )
{
	if( confirm( "Are you sure you want to delete this field?" ) )
	{
		form.editCommand.value = "Delete";
		form.submit();
	}
}

function ModifyField( form )
{
	if( confirm( "Are you sure you want to modify this field?" ) )
	{
		form.editCommand.value = "Modify";
		form.submit();
	}
}

function InsertField( form )
{
	if( confirm( "Are you sure you want to insert a new field?" ) )
	{
		form.editCommand.value = "Insert";
		form.submit();
	}
}

function ChangeField( form )
{
	form.editCommand.value = "Change";
	form.submit();
}
//-->
</SCRIPT>
</head>

<body>
<?
require_once( "./include/navigator.inc" );

if( $_POST["editCommand"] == "Delete" )
{
  DeleteField( $_POST["editField"] );
}
else if( $_POST["editCommand"] == "Modify" )
{
  if( ModifyField( $_POST["editField"], $_POST["editName"], 
               $_POST["editDescription"], substr($_POST["editBGColor"], 1) ) != 0 )
    print( "修改失敗!" );
  else
    print( "修改成功!" );
}
else if( $_POST["editCommand"] == "Insert" )
{
  if( InsertField( $_POST["editName"], $_POST["editDescription"], substr($_POST["editBGColor"], 1) ) != 0 )
    print( "新增失敗!" );
  else
    print( "新增成功!" );
}
?>
<form action="FieldManagement.php" method="post" enctype="multipart/form-data" name="FieldForm">
  <input type="hidden" name="editCommand">
  <table>
	<tr><td><select name="editField" onChange="ChangeField(FieldForm);">
	<?
	$fields_result = GetAllFields();
	while( $row = mysql_fetch_array( $fields_result ) )
	{
		print( "<option value=\"" . $row["AutoID"] . "\"" );
		if( $_POST["editCommand"] == "Change" && $_POST["editField"] == $row["AutoID"] )
		{
			print( " selected" );
			$FieldName = $row["Name"];
			$FieldDescription = $row["Description"];
			
			if( $row["BGColor"] != "" )
				$FieldBGColor = "#" . $row["BGColor"];
		}
		print( ">" . $row["Name"] . "</option>\n" );
	}
	?>
	</select></td></tr>
	<tr>
	  <td>類別名稱:</td>
	  <td><input type="text" name="editName" value="<? print($FieldName); ?>"></td>
	</tr>
	<tr>
	  <td>類別描述:</td>
	  <td><input type="text" name="editDescription" value="<? print($FieldDescription); ?>"></td>
	</tr>
	<tr>
	  <td>類別顏色:</td>
	  <td><div id="colorpicker_wrapper" style="display:none;"></div>
	  <input type="text" name="editBGColor" id="editBGColor" value="<? print($FieldBGColor); ?>" readonly="1">
	  <span id="preview_editBGColor" style="background-color: <? print($FieldBGColor); ?>">preview</span>
	  <script type="text/javascript" src="js/EtColorPicker.js"></script>
	  <script type="text/javascript">
	  <!--
		  ColorPicker.init('colorpicker_wrapper');
		  ColorPicker.bind(['editBGColor'],function(color){
		    var curTarget = ColorPicker.target;
            var spanPreview = document.getElementById('preview_' + curTarget.id);
            curTarget.value = spanPreview.style.backgroundColor = color;
	      });
	  //-->
	  </script>
	  </td>
	</tr>
	<tr><td>
	<input type="button" value="刪除" onClick="DeleteField(FieldForm)">
	<input type="button" value="修改" onClick="ModifyField(FieldForm)">
	<input type="button" value="新增" onClick="InsertField(FieldForm)">
	</td></tr>
  </table>
</form>
<?
require_once( "./include/tail.inc" );
?>
</body>
</html>
